/*
 * DateMan.java
 * Created on 06. Mai 2003, 09:10
 * by chrmac@gmx.de
 * --------
 * This Class makes all date claculations, UNIX<->String
 * 
 */

/**
 * This Class represents the date manger.
 */

package com.jtalker.client;

import java.util.Calendar;

public class DateMan {
	java.util.Date myDate;
	long unixTime = 0; // Unix time in ms
	String yearS = ("0000");
	String monthS = ("00");
	String dayS = ("00");
	String hourS = ("00");
	String minS = ("00");
	String secS = ("00");

	int day = 0, month = 0, year = 0;
	int sec = 0, min = 0, hour = 0;

	/**
	 * Default C'tor. Sets the time to current.
	 */
	public DateMan() {
		this.setTime(-1);
	}

	/**
	 * This C'tor allows You to set a specific time-stamp in seconds.
	 */
	public DateMan(long _unixTime) {
		this.setTime(_unixTime);
	}

	/**
	 * Sets the time of this object. The param is a Unix-timestamp in seconds
	 */
	public void setTime(long _unixTime) {
		this.unixTime = _unixTime * 1000;

		if (this.unixTime >= 0) {
			myDate = new java.util.Date(this.unixTime);
			Calendar cal = Calendar.getInstance();
			cal.setTime(myDate);
			this.year = cal.get(Calendar.YEAR);
			this.month = cal.get(Calendar.MONTH) + 1;
			this.day = cal.get(Calendar.DAY_OF_MONTH);
			this.hour = cal.get(Calendar.HOUR_OF_DAY);
			this.min = cal.get(Calendar.MINUTE);
			this.sec = cal.get(Calendar.SECOND);

			yearS = Integer.toString(this.year);

			if (this.month >= 10)
				monthS = Integer.toString(this.month);
			else
				monthS = "0" + Integer.toString(this.month);

			if (this.day >= 10)
				dayS = Integer.toString(this.day);
			else
				dayS = "0" + Integer.toString(this.day);

			if (this.hour >= 10)
				hourS = Integer.toString(this.hour);
			else
				hourS = "0" + Integer.toString(this.hour);

			if (this.min >= 10)
				minS = Integer.toString(this.min);
			else
				minS = "0" + Integer.toString(this.min);

			if (this.sec >= 10)
				secS = Integer.toString(this.sec);
			else
				secS = "0" + Integer.toString(this.sec);
		} else {
			myDate = new java.util.Date();
			Calendar cal = Calendar.getInstance();
			cal.setTime(myDate);
			this.unixTime = cal.getTimeInMillis();
			this.setTime(this.unixTime / 1000);
		}
	}

	/**
	 * Sets the time of this object.
	 * 
	 * @param _year
	 *            The Year (eg.: "1999")
	 * @param _month
	 *            The Month, begins with 0 (eg.: "2" := March)
	 * @param _date
	 *            The day (eg.: "12")
	 * @param _hour
	 *            The Hour (eg.: "23")
	 * @param _minute
	 *            The Minute (eg.: "59")
	 * @param _second
	 *            The Second (eg.: "1")
	 * */
	public void setTime(int _year, int _month, int _date, int _hour,
			int _minute, int _second) {
		Calendar cal = Calendar.getInstance();
		cal.set(_year, _month, _date, _hour, _minute, _second);

		this.year = cal.get(Calendar.YEAR);
		this.month = cal.get(Calendar.MONTH) + 1;
		this.day = cal.get(Calendar.DAY_OF_MONTH);
		this.hour = cal.get(Calendar.HOUR_OF_DAY);
		this.min = cal.get(Calendar.MINUTE);
		this.sec = cal.get(Calendar.SECOND);

		yearS = Integer.toString(this.year);

		if (this.month >= 10)
			monthS = Integer.toString(this.month);
		else
			monthS = "0" + Integer.toString(this.month);

		if (this.day >= 10)
			dayS = Integer.toString(this.day);
		else
			dayS = "0" + Integer.toString(this.day);

		if (this.hour >= 10)
			hourS = Integer.toString(this.hour);
		else
			hourS = "0" + Integer.toString(this.hour);

		if (this.min >= 10)
			minS = Integer.toString(this.min);
		else
			minS = "0" + Integer.toString(this.min);

		if (this.sec >= 10)
			secS = Integer.toString(this.sec);
		else
			secS = "0" + Integer.toString(this.sec);

		this.unixTime = cal.getTimeInMillis();
	}

	/**
	 * Sets objects time _offset seconds earlier or later
	 * */
	public void offsetTime(long _offset) {
		this.setTime((long) (this.unixTime / 1000) + _offset);
	}

	/**
	 * Gets the time as formatted String. for example: getTime(0) returns
	 * "2003-05-05 09:38:33"
	 */
	public String getTime(int style) {
		if (this.unixTime < 0)
			return "undef";

		if (style == 0)
			return (yearS + "-" + monthS + "-" + dayS + " " + hourS + ":"
					+ minS + ":" + secS);

		if (style == 1)
			return (yearS + "-" + monthS + "-" + dayS);

		if (style == 2)
			return (hourS + ":" + minS + ":" + secS);

		if (style == 3)
			return (hourS + ":" + minS);

		if (style == 4)
			return (minS + ":" + secS);

		if (style == 5)
			return (monthS + "-" + dayS + " " + hourS + ":" + minS);

		return "none";
	}

	/**
	 * Gets the UNIX-TimeStamp.
	 * 
	 * @param _style
	 *            0: Seconds, 1: Milis
	 */
	public long getTimeStamp(int _style) {
		if (_style == 0)
			return (long) (this.unixTime / 1000);
		else
			return this.unixTime;
	}
}
